package com.example.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.Domain.Inform;
import com.example.Domain.User;
import com.example.Util.JwtUtil;
import com.example.common.ServerResponse;
import com.example.service.Impl.InformServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Inform")
public class InformController {
    @Autowired
    private InformServiceImpl informService;
    /*通知查询全部*/
    @RequestMapping("/informAll")
    public ServerResponse informAll(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            System.out.println(jsonObject+":jsonObject");
            System.out.println(data+":data");
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<Inform> a=informService.selectAll(pageNum, pageSize);
            PageInfo<Inform> pageInfo = new PageInfo<>(a);
            System.out.println(pageInfo);

            return ServerResponse.createBySuccess("查询成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }
    /*通知查询已读 状态：0*/
    @RequestMapping("/selectstatezero")
    public ServerResponse selectstatezero(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            System.out.println(jsonObject+":jsonObject");
            System.out.println(data+":data");
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<Inform> a=informService.selectstatezero(pageNum, pageSize);
            PageInfo<Inform> pageInfo = new PageInfo<>(a);
            System.out.println(pageInfo);

            return ServerResponse.createBySuccess("查询成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }
    /*通知查询未读 状态：1*/
    @RequestMapping("/selectstateone")
    public ServerResponse selectstateone(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            System.out.println(jsonObject+":jsonObject");
            System.out.println(data+":data");
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<Inform> a=informService.selectstateone(pageNum, pageSize);
            System.out.println(a);
            PageInfo<Inform> pageInfo = new PageInfo<>(a);
            System.out.println(pageInfo);

            return ServerResponse.createBySuccess("查询成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }
    /*通知查询多条件*/
    @RequestMapping("/selectdim")
    public ServerResponse selectdim(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            String informName = jsonObject.getString("informName");
            Inform inform = new Inform();
            inform.setInformName(informName);
            PageHelper.startPage(pageNum, pageSize);
            System.out.println(inform);
            List<Inform> a=informService.selectdim(inform);
            System.out.println(a);
            PageInfo<Inform> pageInfo = new PageInfo<>(a);
            System.out.println(pageInfo+":::=======================================");


            return ServerResponse.createBySuccess("查询成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }
    /*
     * 删除通知
     * */
    @RequestMapping("/deleteinform")
    public ServerResponse deleteinform(HttpServletRequest request,Integer id){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            boolean a=informService.deleteinform(id);
            return ServerResponse.createBySuccess("删除成功",a);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("删除失败");
        }

    }
    /*
    * 发布通知
    * */
    @RequestMapping("/insertinfrom")
    public ServerResponse insertinfrom(@RequestBody String Data, HttpServletRequest request,Inform inform){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject=JSONObject.parseObject(Data);
            String informName=jsonObject.getString("informName");
            Date informTime=jsonObject.getDate("informTime");
            String informDescribe=jsonObject.getString("informDescribe");
            String informPdf=jsonObject.getString("informPdf");
            Integer informState=jsonObject.getInteger("informState");
            Integer informPeople=jsonObject.getInteger("informPeople");
            String informAccept=jsonObject.getString("informAccept");
           Integer id=u.getId();
           inform.setUserId(id);
           inform.setInformName(informName);
           inform.setInformTime(informTime);
           inform.setInformDescribe(informDescribe);
           inform.setInformPdf(informPdf);
           inform.setInformState(informState);
           inform.setInformPeople(informPeople);
           inform.setInformAccept(informAccept);

            System.out.println(inform);
            boolean a=informService.insertinfrom(inform);

            return ServerResponse.createBySuccess("成功",a);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }

    }
}
